﻿using ExpenseSystem.Common;
using ExpenseSystem.Common.BaseModel;
using ExpenseSystem.Lor;
using ExpenseSystem.Model;
using ExpenseSystem.Fether;
using System.Web;
using System.Reflection;

namespace ExpenseSystem.Core
{
    public  class MergeHelper
    {
        Inhibition inhibition;
        protected string tran = HttpContext.Current.Items[Definition.SESSION_TRAN_STR].ToString();
        public MergeHelper(Inhibition inhibition)
        {
            this.inhibition = inhibition;
        }
        public T SystemObjMerge<T>(T model,SystemFetcher<T> fetcher)where T:SystemModel,new()
        {
            string message;
            T obj = fetcher.Get( model.AutoId, out message);
            if(obj==null)
            {
                obj = new T();
            }
            obj.ResetUpdateTrace();
            foreach(PropertyInfo property in typeof(T).GetProperties())
            {
                //if(!inhibition.Fields.Contains(property.Name)
                //    &&model.PropertyUpdated.Contains(property.Name))
                //{
                //    property.SetValue(obj, property.GetValue(model));
                //}

                if (!inhibition.Fields.Contains(property.Name))
                {
                    property.SetValue(obj, property.GetValue(model));
                }
            }
            return obj;
        }
        public T NormalObjMerge<T>(T model,NormalFetcher<T> fetcher)where T:NormalModel,new()
        {
            string message;
            T obj = fetcher.Get( model.Site, model.AutoId, out message);
            if(obj==null)
            {
                obj = new T();
            }
            obj.ResetUpdateTrace();
            foreach (PropertyInfo property in typeof(T).GetProperties())
            {
                if (!inhibition.Fields.Contains(property.Name))
                {
                    property.SetValue(obj, property.GetValue(model));
                }
            }
            return obj;
        }
    }
}
